package com.dszy.util;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回对象R
 *
 * @author makejava
 * @since 2023-07-12 10:59:00
 */
@Data
public class DsJsonResult implements Serializable {
    private int code;
    private String msg;
    private Object data;

    /**
     * 设置数据
     *
     * @param data 数据
     * @return R
     */
    public DsJsonResult setData(Object data) {
        this.data = data;
        return this;
    }

    /**
     * 操作成功
     *
     * @return R
     */
    public static DsJsonResult ok() {
        DsJsonResult jsonResult = new DsJsonResult();
        jsonResult.code = 200;
        jsonResult.msg = "OK";
        return jsonResult;
    }

    /**
     * 认证授权失败。 包括密钥信息不正确；数字签名错误；授权已超时
     *
     * @return R
     */
    public static DsJsonResult fail() {
        DsJsonResult jsonResult = new DsJsonResult();
        jsonResult.code = 401;
        jsonResult.msg = "fail";
        return jsonResult;
    }

    /**
     * 系统异常
     *
     * @return R
     */
    public static DsJsonResult exp() {
        DsJsonResult jsonResult = new DsJsonResult();
        jsonResult.code = 500;
        jsonResult.msg = "exception";
        return jsonResult;
    }

    @Override
    public String toString() {
        return "{\"code\":" + code + "," +
                "\"msg\":\"" + msg + "\"," +
                "\"data\":\"" + data + "\"}";
    }
}